<template>
	<div class="system-user-container layout-padding">
		<el-card shadow="hover" class="layout-padding-auto">
			<div class="system-user-search mb15">
				<el-input size="default" placeholder="请输入型号名称" style="max-width: 180px" v-model="name"> </el-input>
				<el-button size="default" type="primary" class="ml10" @click="searchList">
					<el-icon>
						<ele-Search />
					</el-icon>
					查询
				</el-button>
				<el-button size="default" type="success" class="ml10" @click="onAddMode('add')">
					<el-icon>
						<ele-FolderAdd />
					</el-icon>
					新增型号
				</el-button>
			</div>
			<el-table :data="state.tableData.data" v-loading="state.tableData.loading" style="width: 100%">
				<el-table-column type="index" label="序号" width="60" />
				<el-table-column prop="name" label="型号名称" show-overflow-tooltip></el-table-column>
				<el-table-column prop="createdAt" label="创建时间" show-overflow-tooltip></el-table-column>
				<el-table-column label="操作" width="130">
					<template #default="scope">
						<el-button :disabled="scope.row.username === 'admin'" size="small" text type="primary" @click="onOpenEditmode('edit', scope.row)"
							>修改</el-button
						>
						<el-button :disabled="scope.row.username === 'admin'" size="small" text type="primary" @click="onRowDel(scope.row)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
		</el-card>
		<equipmentDialog ref="equipmentDialogRef" @refresh="getTableData()" />
		<equipmentEditDialog ref="equipmentEditDialogRef" @refresh="getTableData()" />
	</div>
</template>

<script setup lang="ts" name="systemUser">
import { defineAsyncComponent, reactive, onMounted, ref } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';

import { useEquipmentmode } from './equipmentmode';
const equipmentmodeStore = useEquipmentmode();

// 引入组件
const equipmentDialog = defineAsyncComponent(() => import('/@/views/equipmentmode/dialog.vue'));
const equipmentEditDialog = defineAsyncComponent(() => import('/@/views/equipmentmode/editDialog.vue'));
// 定义变量内容
const equipmentDialogRef = ref();
const equipmentEditDialogRef = ref();
const state = reactive({
	tableData: {
		data: [],
		total: 0,
		loading: false,
		param: {
			pageNum: 1,
			pageSize: 10,
		},
	},
});
const name = ref('');

// 初始化表格数据
const getTableData = () => {
	state.tableData.loading = true;
	equipmentmodeStore
		.getEquipmentmodeList(name.value)
		.then((res) => {
			state.tableData.data = res;
		})
		.catch((err) => {
			console.log(err);
		})
		.finally(() => {
			state.tableData.loading = false;
		});
};
// 打开新增类型弹窗
const onAddMode = (type: string) => {
	equipmentDialogRef.value.openDialog(type);
};
// 打开修改用户弹窗
const onOpenEditmode = (type: string, row: any) => {
	equipmentEditDialogRef.value.openDialog(type, row);
};

// 删除用户
const onRowDel = (row: any) => {
	ElMessageBox.confirm(`此操作将删除型号：“${row.name}”，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
	})
		.then(() => {
			equipmentmodeStore
				.deleteEquipmentmode(row.id)
				.then(() => {
					getTableData();
					ElMessage.success('删除成功');
				})
				.catch((err) => {
					ElMessage.error(err.message);
				});
		})
		.catch(() => {});
};
// 分页改变
// const onHandleSizeChange = (val: number) => {
// 	state.tableData.param.pageSize = val;
// 	getTableData();
// };
const searchList = () => {
	state.tableData.param.pageNum = 1;
	getTableData();
};
// 分页改变
// const onHandleCurrentChange = (val: number) => {
// 	state.tableData.param.pageNum = val;
// 	getTableData();
// };
// 页面加载时
onMounted(() => {
	getTableData();
});
</script>

<style scoped lang="scss">
.system-user-container {
	:deep(.el-card__body) {
		display: flex;
		flex-direction: column;
		flex: 1;
		overflow: auto;
		.el-table {
			flex: 1;
		}
	}
}
</style>
